
import {Net} from './net'

export class UIQue{
    constructor(tasks) {
        this.tasks = tasks || []
    }

    finished(message){
        console.log('que finshed')
    }
    
    failed(message){
        console.log('que failed')
    }

    set data(value){
        this._data = value
    }
    get data(){
        return this._data || {}
    }

    set tasks(value){
        this._tasks = value
    }

    get tasks(){
        return this._tasks || [new Net()]
    }

    set vue(value){
        this._vue = value
    }

    get vue(){
        return this._vue || {}
    }

    set que_num(value){
        if (value !== this.que_num)
        {
            this._que_num = value

            if (value === 0){
                this.finished(this.que_message)
            }
            if (value === -1){
                this.failed(this.que_message)
            }
        }
    }

    get que_num(){
        return this._que_num || -2 //-2|waiting   -1|failed    0|finished
    }

    set que_message(value){
        console.log(value);
        this._que_message = value
    }

    get que_message(){
        return this._que_message || ''
    }
    
    start(){
        let ws = this
        let proxyTasks = []
        let i = 0
        this.tasks.forEach(element => {
            if (element instanceof Net){
                element.tag = element.tag || `${i}`
                element.need = element.need || true
                i++;
                let proxy = ws._buildNetProxy(element)
                proxyTasks.push(proxy)
            }
            else{
                throw Error('object in tasks is must one instance of Net')
            }
        });
        if (this.tasks.length < 1){
            throw Error('please add one Net instance to tasks')
        }
        this.data = {}
        this.que_num = this.tasks.length
        proxyTasks.forEach(net => {
            net.send()
        })
    }
    
    _buildNetProxy(net){
        let ws = this
        let proxy = new Proxy(net,{
            get:function (target, key, receiver) {
                if (key === 'successBlock')
                {
                    let block = target[key]
                    let proxyBlock = function(resp){
                        try {
                            block(resp) 
                            //  resp.data
                            ws.data[target.tag] = resp.data
                            ws.que_message = `${resp.message}`
                            ws.que_num = ws.que_num - 1
                            
                        } catch (error) {
                            if (target.need)
                            {
                                block(resp) 
                                ws.que_message = `${error}`
                                ws.que_num = -1
                            }else{
                                block(resp)
                                ws.data[target.tag] = {}
                                ws.que_message = `数据加载成功`
                                ws.que_num = ws.que_num - 1
                            }
                        }
                    }
                    return proxyBlock

                }else if( key === 'rejectBlock'){
                    let block = target[key]
                    let proxyBlock = function(resp){
                        try {
                            if (!target.need){
                                block(resp) 
                                ws.data[target.tag] = {}
                                ws.que_message = `数据加载成功`
                                ws.que_num = ws.que_num - 1
                            }
                            else{
                                block(resp) 
                                ws.que_message = `${resp.message}`
                                ws.que_num = -1
                            }
                        } catch (error) {
                            block(resp) 
                            ws.que_message = `${error}`
                            ws.que_num = -1
                        }
                    }
                    return proxyBlock
                }
                return target[key] || undefined
            },
        })
        return proxy
    }
   
}
let one = new UIQue()
let nt1 = new Net().GET.url('http://iwe.doncheng.cn/apiv1/culture/xishanproject').then(function(resp){
    // console.log('net 1 OK|')
}).catch(function(error){
    // console.log('net 1 error|')
})

let nt2 = new Net().GET.url('http://iwe.doncheng.cn/apiv1/events/days').then(function(resp){
    // console.log('net 2 OK|')
}).catch(function(error){
    // console.log('net 2 error|')
})

let nt3 = new Net().GET.url('http://iwe.doncheng.cn/apiv1/culture/dianchiroom').then(function(resp){
    // console.log('net 2 OK|')
}).catch(function(error){
    // console.log('net 2 error|')
})

let nt4 = new Net().GET.url('http://iwe.doncheng.cn/apiv1/events/days').then(function(resp){
    // console.log('net 2 OK|')
}).catch(function(error){
    // console.log('net 2 error|')
})

one.tasks = [nt1,nt2,nt3]
one.finished = function(){
    
}
one.start()



// console.log(newNet.age)
// console.log(newNet.method)


